package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.atguigu.tingshu.vo.account.AccountDeductVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;

	@Override
	public IPage<UserAccountDetail> findUserConsumePage(Page<UserAccountDetail> userAccountDetailPage, Long userId) {
		//	构建查询条件
		LambdaQueryWrapper<UserAccountDetail> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(UserAccountDetail::getUserId,userId)
				.eq(UserAccountDetail::getTradeType,SystemConstant.ACCOUNT_TRADE_TYPE_MINUS)
				.orderByDesc(UserAccountDetail::getId);
		//	调用mapper 层方法
		return userAccountDetailMapper.selectPage(userAccountDetailPage,wrapper);
	}

	@Override
	public IPage<UserAccountDetail> findUserRechargePage(Page<UserAccountDetail> userAccountDetailPage, Long userId) {
		//	构建查询条件
		LambdaQueryWrapper<UserAccountDetail> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(UserAccountDetail::getUserId,userId)
				.eq(UserAccountDetail::getTradeType,SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT)
				.orderByDesc(UserAccountDetail::getId);
		//	调用mapper 层方法
		return userAccountDetailMapper.selectPage(userAccountDetailPage,wrapper);
	}

	@Override
	public void addUserAccountDetail(Long userId, String title, String type, BigDecimal amount, String orderNo) {
		//	创建对象
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(userId);
		userAccountDetail.setTitle(title);
		userAccountDetail.setTradeType(type);
		userAccountDetail.setAmount(amount);
		userAccountDetail.setOrderNo(orderNo);
		//	保存数据
		userAccountDetailMapper.insert(userAccountDetail);
	}

	@Override
	public Boolean checkAndDeduct(AccountDeductVo accountDeductVo) {
		//	检查扣减金额是否成功;
        try {
			//	执行方法
            int result = userAccountMapper.checkAndDeduct(accountDeductVo.getUserId(),accountDeductVo.getAmount());
			//	记录用户消费记录;
			addUserAccountDetail(accountDeductVo.getUserId(),accountDeductVo.getContent(), SystemConstant.ACCOUNT_TRADE_TYPE_MINUS,accountDeductVo.getAmount(),accountDeductVo.getOrderNo());
			//	返回真假
			return result > 0;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
	}

	@Override
	public BigDecimal getAvailableAmount(Long userId) {
		//	获取账户可用金额！
		return userAccountMapper.selectOne(new LambdaQueryWrapper<UserAccount>().eq(UserAccount::getUserId, userId)).getAvailableAmount();
	}

	@Override
	public void initAccount(Long userId) {
		//	创建一个对象
		UserAccount userAccount = new UserAccount();
		//	赋值：
		userAccount.setUserId(userId);
		userAccount.setTotalAmount(new BigDecimal("100"));
		userAccount.setAvailableAmount(new BigDecimal("100"));
		//	保存数据;
		userAccountMapper.insert(userAccount);
	}
}
